<!-- 小组管理 -->
<template>
  <div>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="小组名称">
        <el-input
          clearable
          size="small"
          v-model="formInline.groupName"
          auto-complete="off"
          placeholder="请输入小组名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="getData">查询</el-button>
        <el-button size="small" type="primary" @click="add()">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      size="small"
      :data="datalist"
      highlight-current-row
      border
      element-loading-text="拼命加载中"
      style="width: 100%;"
    >
      <el-table-column align="center" sortable prop="groupName" label="小组名称" min-width="150"></el-table-column>
      <el-table-column align="center" sortable prop="total" label="小组人数" min-width="100"></el-table-column>
      <el-table-column align="center" sortable prop="dutyCount" label="在岗人数" min-width="100"></el-table-column>
      <el-table-column align="center" sortable prop="leader" label="组长" min-width="100"></el-table-column>
      <el-table-column align="center" sortable prop="status" label="状态" min-width="100">
        <template
          slot-scope="scope"
        >{{scope.row.status == 1 ? '出警':scope.row.status == 3 ? '空闲':"训练"}}</template>
      </el-table-column>
      <el-table-column align="center" sortable prop="lastTime" label="上次出警时间" min-width="200"></el-table-column>
      <el-table-column align="center" label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="danger" @click="del(scope.row)">删除</el-button>
          <el-button size="mini" type="primary" @click="update(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="page-box"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="pageparm.currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageparm.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageparm.total"
    ></el-pagination>
    <el-dialog :title="title" :visible.sync="addVisible" width="40%" @click="closeDialog()">
      <el-form ref="addData" :rules="configRules" :model="addData" label-width="100px">
        <el-form-item label="小组名称" prop="groupName">
          <el-input size="small" v-model="addData.groupName" auto-complete="off" placeholder="小组名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog()">取消</el-button>
        <el-button size="small" type="primary" class="title" @click="addGroup()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getList, add, update, deleteGroup } from "../../api/xiaofang/group";
export default {
  data() {
    return {
      addData: {},
      configRules: {
        groupName: [{ required: true, message: "必填", trigger: "change" }]
      },
      pageparm: {
        currentPage: 0,
        pageSize: 10,
        total: 0
      },
      datalist: [],
      formInline: {},
      title: "",
      addVisible: false
    };
  },
  created() {
    this.getData();
  },
  methods: {
    del(row) {
      deleteGroup(row.id).then(res => {
        if (res.status == 200) {
          this.$message.success("删除成功");
          this.getData();
        } else {
          this.$message.error(res.info);
        }
      });
    },
    update(row) {
      this.addData = row;
      this.addVisible = true;
      this.title = "修改小组";
    },
    addGroup() {
      this.$refs.addData.validate(valid => {
        if (valid) {
          if (this.addData.id) {
            update(this.addData).then(res => {
              if (res.status == 200) {
                this.$message.success("修改成功");
                this.addVisible = false;
                this.getData();
              } else {
                this.$message.error(res.info);
              }
            });
          } else {
            add(this.addData).then(res => {
              if (res.status == 200) {
                this.$message.success("保存成功");
                this.addVisible = false;
                this.getData();
              } else {
                this.$message.error(res.info);
              }
            });
          }
        }
      });
    },
    closeDialog() {
      this.addVisible = false;
    },
    add() {
      this.addData = {};
      this.addVisible = true;
      this.title = "新增小组";
    },
    handleCurrentChange(val) {
      this.pageparm.currentPage = val;
      this.getData();
    },
    handleSizeChange(val) {
      this.pageparm.pageSize = val;
      this.getData();
    },
    getData() {
      this.formInline.pageIndex = this.pageparm.currentPage;
      this.formInline.pageSize = this.pageparm.pageSize;
      getList(this.formInline).then(res => {
        this.datalist = res.records;
        this.pageparm.total = res.total;
      });
    }
  }
};
</script>